<?php

namespace Business;

use Lib\Core\Business;

class User extends Base {

    /**
     * 验证手机号后发送验证码
     *
     * @param $phone
     * @param $mask
     * @param $ip
     * @throws \Lib\Exception\Business
     */
    public function checkPhoneAndSendMessageVerify ($phone, $mask, $ip) {
        $id = \Data\User::instance()->getUidByPhoneNum($phone);
        if($id) {
            $this->throwException('该手机号已经被注册过', \Entity\ErrorCode::PHONE_NUMBER_EXIST);
            return false;
        } else {
            $this->sendMessageVerify($mask, $phone, $ip);
            return true;
        }
    }

    /**
     * 设置用户验证码.
     *
     * @param string $userMask 用户cookie唯一标识.
     * @param string $val 验证码的值.
     *
     * @return void
     */
    public function setVerify($userMask, $val) {
        \Data\Verify::instance()->setVerify($userMask, $val);
    }

    /**
     * 得到用户的验证码.
     *
     * @param string $userMask 用户cookie唯一标识.
     *
     * @return string
     */
    public function getVerify($userMask) {
        return \Data\Verify::instance()->getVerify($userMask);
    }

    public function registerByPhone($phoneNum, $password, $nickname, $mask, $ip) {
        $password = \Util\User::encodePassword($password);
        $data = array(
            'PhoneNum' => $phoneNum,
            'Password' => $password,
            'NickName' => $nickname
            );
        $uid = \Data\User::instance()->addUser($data);
        \Data\User::instance()->setUserLoginMask($mask, $uid);//用户登录
        return $uid;
    }

    /**
     * 发送验证码.
     *
     * @param string  $mask
     * @param integer $phone
     * @param string  $ip
     *
     * @throws \Lib\Exception\Business
     */
    public function sendMessageVerify($mask, $phone, $ip) {
        $dataVerify = \Data\Verify::instance();
        // TODO 验证cookie
        $numberInfo = \Data\Verify::instance()->getSendVerifyNumber($mask, 'mask');
        $ttl = $numberInfo['ttl'];
        $dayNumber = $numberInfo['dayNumber'];
        if ($ttl > 0) {
            $this->throwException('60秒内发送过验证码，请等待' . $ttl . '秒', \Entity\ErrorCode::SEND_VERIFY_ERROR);
        }
        if ($dayNumber > 20) {
            $this->throwException('今天发送验证码已经超过20次了' . \Entity\ErrorCode::SEND_VERIFY_ERROR);
        }
        // TODO 验证手机号
        $numberInfo = \Data\Verify::instance()->getSendVerifyNumber($mask, 'phone');
        $ttl = $numberInfo['ttl'];
        $dayNumber = $numberInfo['dayNumber'];
        if ($ttl > 0) {
            $this->throwException('你的手机60秒内发送过验证码，请等待' . $ttl . '秒', \Entity\ErrorCode::SEND_VERIFY_ERROR);
        }
        if ($dayNumber > 20) {
            $this->throwException('你的手机今天发送验证码已经超过20次了' . \Entity\ErrorCode::SEND_VERIFY_ERROR);
        }
        // TODO 验证ip
        $numberInfo = \Data\Verify::instance()->getSendVerifyNumber($mask, 'ip');
        $dayNumber = $numberInfo['dayNumber'];
        if ($dayNumber > 200) {
            $this->throwException('你的ip今天发送验证码已经超过200次了' . \Entity\ErrorCode::SEND_VERIFY_ERROR);
        }
        // TODO 发送验证码
        $code = $dataVerify->getMessageCode($mask, $phone);
        if (!$code) {
            $code = mt_rand(100000, 999999);
        }
        $dataVerify->setMessageCode($mask, $phone, $code);
        \Util\Message::sendVerify($phone, $code);
        // 计数器
        $dataVerify->incSendVerifyNumber($mask, 'mask');
        $dataVerify->incSendVerifyNumber($phone, 'phone');
        $dataVerify->incSendVerifyNumber($ip, 'ip');
    }

    /**
     * 获取短信验证码.
     *
     * @param string $mask
     * @param string $phone
     *
     * @return string
     */
    private function getMessageVerify($mask, $phone) {
        $code = \Data\Verify::instance()->getMessageCode($mask, $phone);
        return $code;
    }

    /**
     * 判断验证码
     *
     * @param $verify
     * @param $mask
     * @param $phone
     * @param bool $isReturn
     * @return bool
     * @throws \Exception
     * @throws \Lib\Exception\Business
     */
    public function checkMessageVerify($verify, $mask, $phone, $isReturn = false) {
        $trueVerify = $this->getMessageVerify($mask, $phone);
        try {
            if (!$trueVerify) {
                $this->throwException('验证码过期', \Entity\ErrorCode::VERIFY_ERROR);
            }
            if (strtolower($trueVerify) != strtolower($verify)) {
                $this->throwException('验证码错误', \Entity\ErrorCode::VERIFY_ERROR);
            }
            return true;
        } catch (\Lib\Exception\Business $e) {
            if ($isReturn) {
                return false;
            }
            throw $e;
        }
    }

    /**
     * 获取当前的用户信息.
     *
     * @param string $mask 用户cookie唯一标识.
     *
     * @return array
     */
    public function getCurrentUserInfo($mask) {
        // 通过mask获取用户id
        $info = \Data\User::instance()->getCurrentUserInfo($mask);
        if ($info) {
            return $info;
        }
    }

    public function getUserInfoByPhone($phone) {
        $userInfo = \Data\User::instance()->getUserInfoByPhone($phone);
        return $userInfo;
    }
}
